/*
 * SDLWrapper.cpp
 *
 * Author: Samuel Queiroz
 */

#include "SDLWrapper.h"

void SDLWrapper::run()
{
	init();

	while(!stop_)
	{
	    while(SDL_PollEvent(&event))
	    {
	    	notifyListeners();
	    }
		leftXAxis = SDL_JoystickGetAxis(joystick, LEFT_XAXIS);
		leftYAxis = -SDL_JoystickGetAxis(joystick, LEFT_YAXIS);
		rightXAxis = SDL_JoystickGetAxis(joystick, RIGHT_XAXIS);
		rightYAxis = -SDL_JoystickGetAxis(joystick, RIGHT_YAXIS);
	}

	quit();
}


void SDLWrapper::init()
{
    /* Initialise SDL */
	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
	{
        fprintf( stderr, "Could not initialise SDL: %s\n", SDL_GetError() );
        exit( -1 );
    }

	this->joystick = SDL_JoystickOpen(0);

    /* Set a video mode */
    if( !SDL_SetVideoMode( 25, 25, 0, 0 ) ){
        fprintf( stderr, "Could not set video mode: %s\n", SDL_GetError() );
        SDL_Quit();
        exit( -1 );
    }

    /* Enable Unicode translation */
    SDL_EnableUNICODE( 1 );
}

void SDLWrapper::quit()
{
    SDL_JoystickClose(joystick);
	SDL_Quit();
}
